package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class implements get category from db
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageGetCategory {
	
	private ConnectionToClient client;
	ArrayList<String> CatList=null;
	/**
	 * constractor of this class 
	 * @param client
	 */
	public MessageGetCategory(ConnectionToClient client) {
		// TODO Auto-generated constructor stub
		this.client=client;
		
	}
	/**
	 * getting the category list from the DB 
	 * @return
	 * @throws IOException
	 */
	public ArrayList<String> getCat() throws IOException 
	{
		
		try {
			SQLConnection sc = new SQLConnection();
			
			PreparedStatement ps = sc.getConnection().prepareStatement("SELECT * FROM categories;");
			ResultSet rs = ps.executeQuery();
	
		
			
			CatList = new ArrayList<String>();
			CatList.add("GetCategory");
			
	        while(rs.next())
	        {	
	        	CatList.add(rs.getString(1));
	        //	System.out.println(rs.getString(1));
	        }
	        
	        client.sendToClient(CatList);
	        
			rs.close();
			return CatList;

		} catch (SQLException e) {
			
			e.printStackTrace();
			return null;
			
		}
	}

	
	
	
}
